package com.monise.view;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import com.monise.dao.DAO.DadosConsultaDAO;
import com.monise.dao.util.ConsultaTableModel;
import com.monise.model.Animal;
import com.monise.model.Cliente;
import com.monise.model.DadosConsulta;

public class TabelaHistorico extends JInternalFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTable table;
	private Animal animal;
	public Cliente cliente;
	private ConsultaTableModel model;
	private FrameConsultaHistorico frameHistorico;

	/**
	 * Launch the application.
	 */
	
	/**
	 * Create the frame.
	 */
	public TabelaHistorico(Animal animal, Cliente cliente,FrameConsultaHistorico frameHistorico ) {
		setTitle("Consultas realizadas");
		
		this.animal = animal;
		this.cliente = cliente;
		this.frameHistorico = frameHistorico;
		
		setClosable(true);
		setBounds(100, 100, 417, 445);
		
		JScrollPane scrollPane = new JScrollPane();
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 377, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(14, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(25)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 342, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(49, Short.MAX_VALUE))
		);
		
		///modeloConsulta.addColumn("Data");
	//	modeloConsulta.addColumn("Valor");
		
		model = new ConsultaTableModel(buscaConsulta());

		table = new JTable(model);
		scrollPane.setViewportView(table);
		getContentPane().setLayout(groupLayout);
		
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					cliqueCodigo();
				}
			}
		});

	//	buscaConsulta();
	}
	
	private List<DadosConsulta> buscaConsulta(){
		if(cliente!=null){
			return buscaConsultaCliente();
		}else{
			return buscaConsultaAnimal();
		}
	}
	
	private List<DadosConsulta> buscaConsultaAnimal(){
		DadosConsultaDAO consultaDao = new DadosConsultaDAO();
		List<DadosConsulta> consultas = consultaDao.getHistoricoConsultas(animal);
		return consultas;
	}
	
	private List<DadosConsulta> buscaConsultaCliente(){
		DadosConsultaDAO consultaDao = new DadosConsultaDAO();
		List<DadosConsulta> consultas = consultaDao.getHistoricoConsultas(cliente);
		return consultas;
	}
	
	
	private void cliqueCodigo() {
		frameHistorico.setConsultaSelecionada(getConsultaSelecionada());
		frameHistorico.preencherDados();
		
		TabelaHistorico.this.setVisible(false);
	}
	
	private DadosConsulta getConsultaSelecionada() {  
	    if (table.getSelectedRow() == -1) {  
	       return null;  
	    }  
	    DadosConsulta consulta = model.get(table.getSelectedRow()); 
	    return consulta;
	}  	
	
}
